<?php
namespace app\api\controller\v1;
use think\facade\Cookie;
use think\Db;
class Subscribe extends Base{
	
	
	public function get_staff_type(){
		$staff_type_list=Db::name('staff_type')->field('id,name')->where(['is_show'=>1,'status'=>1])->order('sort asc')->select();
		return toSuccess('查询成功',$staff_type_list);
	}
	
	/*员工预约*/
	public function add_user_subscribe(){
		$data=input('post.');
		if(empty($data['name'])){
			return toError(0,'姓名不能为空');
		}
		if(empty($data['mobile'])){
			return toError(0,'手机号不能为空');
		}
		if(!preg_match("/^1[34578]\d{9}$/", $data['mobile'])){
			return toError(0,'请输入正确手机号');
		}
		if(empty($data['address']) || empty($data['long']) || empty($data['lat'])){
			return toError(0,'地址信息错误');
		}
		if(empty($data['item_ids'])){
			return toError(0,'请选择预约项目');
		}
		if(empty($data['user_id'])){
			return toError(0,'操作错误');
		}
		$data['add_time']=time();
		$data['order']=get_order();
		if(Db::name('subscribe')->insert($data)){
			return toSuccess('提交成功，稍后将会有客服联系您');
		}else{
			return toError(202,'操作失败，请稍后再试');
		}
	}
	
	
	public function subscribe_list(){
		$data=input('post.');
		if(empty($data['user_id'])){
			return toError(0,'操作错误');
		}
		$where=[];
		if($data['type']==1){
			$where["status"]=1;
		}
		if($data['type']==2){
			$where["status"]=2;
		}
		
		$page=input('page',1);
		$limit=input('limit',10);
		$subscribe=Db::name('subscribe')->where('user_id',$data['user_id'])->field("*,FROM_UNIXTIME(add_time,'%Y-%m-%d') as add_time")->page($page,$limit)->select();
		
		foreach($subscribe as $k=>$v){
			$subscribe[$k]['staff_type_name']=Db::name('staff_type')->where('id','in',$v['item_ids'])->column('name');
			if(Db::name('offer')->where(['s_id'=>$v['id'],'status'=>2])->find()){
				$subscribe[$k]['status']=3;
			}
		}
		
		$conut=Db::name('subscribe')->where('user_id',$data['user_id'])->count();
		return toSuccess('获取成功',$subscribe,$conut,ceil($conut/$limit));
	}
	
	
	public function subscribe_info(){
		$data=input('post.');
		if(empty($data['user_id'])){
			return toError(0,'操作错误');
		}
		if(empty($data['s_id'])){
			return toError(0,'s_id操作错误');
		}
		$info=Db::name('subscribe')->where('id',$data['s_id'])->where('user_id',$data['user_id'])->find();
		if(empty($info)){
			return toError(0,'s_id操作错误');
		}
		$data1['open']=0;
		if($info['is_grade']==0){
			if($info["status"]!=0){
				$process_info=Db::name('process')->where(['s_id'=>$data['s_id'],'t_id'=>5])->order('add_time desc')->find();
				if($process_info['status']==0 || $process_info['status']==1){
					$data1['plan']=1;
					if(!empty($process_info['visit_time'])){
						$data1['visit_time']=$process_info['visit_time'];
					}
				}
				//发送测量结果，与测量上传的图片
				$data1['measure_list']=Db::name('measure')->field("id,status,mianji,huxing,FROM_UNIXTIME(add_time,'%Y-%m-%d') as add_time")->where(['s_id'=>$data['s_id'],'user_id'=>$process_info['user_id']])->where('status','4')->order('id asc')->find();
			
				if($process_info['status']==2){ #表示测量已经ok了
					
					$process_info6=Db::name('process')->where(['s_id'=>$data['s_id'],'t_id'=>6])->order('add_time desc')->find();
					if($process_info6['status']==0 || $process_info6['status']==1){
						$data1['plan']=2;
						$data1['open']=1;
					}
					//发送报价结果，与测量上传的图片
					$data1['offer_list']=Db::name('offer')->field("id,status,mianji,huxing,reason,FROM_UNIXTIME(add_time,'%Y-%m-%d') as add_time,money")->where('status','in','2,4,5')->where(['s_id'=>$data['s_id'],'user_id'=>$process_info6['user_id']])
										->order('id asc')->select();
					if($process_info6['status']==2){
						$data1['plan']=3;
					}
				}
				
			}else{
				$data1['plan']=0;
			}
		}else{
			$data1['plan']=3;
		}
		$data1['staff_type_name']=Db::name('staff_type')->where('id','in',$info['item_ids'])->column('name');
		$data1['status']=$info['status'];
		$data1['name']=$info['name'];
		$data1['address']=$info['address'];
		$data1['long']=$info['long'];
		$data1['lat']=$info['lat'];
		$data1['order']=$info['order'];
		$data1['remark']=$info['remark'];
		return toSuccess('获取成功',$data1);
		
	}
	
	
	public function audit_booking(){
		$data=input('post.');
		if(empty($data['user_id'])){
			return toError(0,'操作错误');
		}
		if(empty($data['s_id'])){
			return toError(0,'s_id操作错误');
		}
		$subscribe=Db::name('subscribe')->where(['id'=>$data['s_id'],'user_id'=>$data['user_id']])->find();
		
		if(empty($subscribe)){
			return toError(0,'s_id操作错误');
		}
		
		$pro_info=Db::name('process')->where(['s_id'=>$data['s_id'],'user_id'=>$subscribe['allot_id']])->find();
		
		if($data['type']==1){
			//审核通过
			Db::name('process')->where(['s_id'=>$data['s_id'],'user_id'=>$subscribe['allot_id']])->update(['update_time'=>time(),'status'=>2]);
			if($pro_info['t_id']==5){
				Db::name('measure')->where(['s_id'=>$data['s_id'],'user_id'=>$subscribe['allot_id']])->update(['update_time'=>time(),'status'=>4]);
			}else{
				Db::name('offer')->where(['id'=>$data['o_id']])->update(['update_time'=>time(),'status'=>4]);
				Db::name('process')->where(["id"=>$pro_info['id']])->update(["update_time"=>time(),'status'=>2]);
				Db::name('subscribe')->where(['id'=>$data['s_id'],'user_id'=>$data['user_id']])->update(['status'=>2,'update_time'=>time()]);
			}
			return toSuccess('操作成功');
		}
		if($data['type']==2){
			//审核不通过
			if(empty($data['reason'])){
				return toError(0,'理由不能为空');
			}
			$imgs=empty($data['imgs']) ? "" : $data['imgs'];
			if($pro_info['t_id']==5){
				Db::name('measure')->where(['s_id'=>$data['s_id'],'user_id'=>$subscribe['allot_id']])->update(['update_time'=>time(),'status'=>5,'reason'=>$data['reason'],'imgs'=>$imgs]);
			}else{
				Db::name('offer')->where(['id'=>$data['o_id']])->update(['update_time'=>time(),'status'=>5,'reason'=>$data['reason'],'imgs'=>$imgs]);
			}
			return toSuccess('操作成功');
		}
	}
	
	
	public function get_kjindu(){
		$data=input('post.');
		if(empty($data['user_id'])){
			return toError(0,'操作错误');
		}
		if(empty($data['s_id'])){
			return toError(0,'s_id操作错误');
		}
		
		$subscribe=Db::name("subscribe")->where("id",$data["s_id"])->find();
		if(empty($subscribe)){
			return toError(0,'s_id操作错误');
		}
		if($subscribe["is_allot"]==0){
			$data1["fuzuren_info"]=Db::name("user")->where("id",$subscribe["allot_id"])->field("mobile,username")->find();
		}
		$data1["open"]=0;
		if($subscribe["is_allot"]==1){
			$t_id=Db::name("team")->where("id",$subscribe["allot_id"])->value("t_id");
			$data1["fuzuren_info"]=Db::name("user")->where("id",$t_id)->field("mobile,username")->find();
			if($subscribe["x_status"]==1){
				$status=Db::name('process')->where(["s_id"=>$data['s_id'],"user_id"=>$t_id])->value("status");
				if($status==2){
					$data1["open"]=1;
				}
			}
		}
		
		
		
		$process_list=Db::name('process p')
				->join('user u','p.user_id=u.id','left')
				->join('staff_type s','p.t_id=s.id','left')->where('p.s_id',$data['s_id'])->order('p.add_time desc')
				->field("p.id pr_id,p.status,u.username,u.head_img,s.name staff_type_name,FROM_UNIXTIME(p.add_time,'%Y-%m-%d %H:%i') add_time")->select();
		foreach($process_list as $k=>$v){
			$process_list[$k]["img"]=Db::name("file_data")->where("p_id",$v["pr_id"])->column("file_src");
		}		
		$data1["process_list"]=$process_list;
		return toSuccess('获取成功',$data1);
	}
	
	
	public function my_xiangmu_list(){
		$data=input('post.');
		$page=input('page',1);
		$limit=input('limit',10);
		if(empty($data['user_id'])){
			return toError(0,'操作错误');
		}
		$where=["user_id"=>$data['user_id'],"is_grade"=>1];
		if(!empty($data['type'])){
			$where["x_status"]=$data['type'];
		}
		$subscribe=Db::name("subscribe")->where($where)->page($page,$limit)->field('id,item_ids,name,address,x_status,allot_id')->select();
		foreach($subscribe as $k=>$v){
			$subscribe[$k]['staff_type_name']=Db::name('staff_type')->where('id','in',$v['item_ids'])->column('name');
			$offer=Db::name("offer")->where(["s_id"=>$v["id"],"status"=>4])->find();
			$t_id=Db::name("team")->where('id',$v['allot_id'])->value('t_id');
			$subscribe[$k]["team_name"]=Db::name("user")->where('id',$t_id)->value('username');
			$subscribe[$k]["money"]=$offer["money"];
			$process_info=Db::name('process')->where(['s_id'=>$v['id'],"user_id"=>$t_id,"status"=>2])->find();
			if(!empty($process_info)){
				$subscribe[$k]["complete_time"]=$process_info["complete_time"];
			}
		}
		$conut=Db::name('subscribe')->where($where)->count();
		return toSuccess('获取成功',$subscribe,$conut,ceil($conut/$limit));
	}
	
	
	public function yan_file_list(){
		$data=input('post.');
		if(empty($data['user_id'])){
			return toError(0,'操作错误');
		}
		if(empty($data['type'])){
			return toError(0,'type操作错误');
		}
		if(empty($data['s_id'])){
			return toError(0,'s_id操作错误');
		}
		
		$subscribe=Db::name("subscribe")->where("id",$data["s_id"])->find();
		if(empty($subscribe)){
			return toError(0,'s_id操作错误');
		}
		$t_id=Db::name("team")->where('id',$subscribe['allot_id'])->value('t_id');
		
		$data1['indoor_type']=Db::name('indoor_type')->where('status',1)->field('id,name')->order('sort asc')->select();
		$data1['file_data_list']=Db::name('file_data')->where(['type'=>$data['type'],'s_id'=>$data['s_id'],"user_id"=>$t_id])->field('id,file_src,indoor_type_id')->select();
		return toSuccess('获取成功',$data1);
	}
	
	
	public function yanshou(){
		$data=input('post.');
		if(empty($data['user_id'])){
			return toError(0,'操作错误');
		}
		if(empty($data['s_id'])){
			return toError(0,'s_id操作错误');
		}
		if(empty($data['type'])){
			return toError(0,'type操作错误'); //1审核通过2审核不通过
		}
		if(empty($data['reason'])){
			return toError(0,'reason操作错误'); //1审核通过2审核不通过
		}
		if($data['type']==1){
			Db::name("subscribe")->where("id",$data['s_id'])->update(["x_status"=>3,"update_time"=>time()]);	
		}
		
		if($data['type']==2){
			$subscribe=Db::name("subscribe")->where("id",$data['s_id'])->find();
			Db::name("subscribe")->where("id",$data['s_id'])->update(["x_status"=>1,"update_time"=>time(),"reason"=>$data['reason']]);	
			$not_ids=Db::name('process')->where(['s_id'=>$subscribe['id'],'team_id'=>$subscribe['allot_id'],'status'=>3])->column('id');
			Db::name('process')->where(['s_id'=>$subscribe['id'],'team_id'=>$subscribe['allot_id']])
			->where('id','not in',$not_ids)->update(["status"=>1,'update_time'=>time()]);
		}
		return toSuccess('操作成功');
	}
	//项目订单详情
	public function order_info(){
		$data=input('post.');
		if(empty($data['user_id'])){
			return toError(0,'操作错误');
		}
		if(empty($data['s_id'])){
			return toError(0,'s_id操作错误');
		}
		$subscribe=Db::name("subscribe")->where("id",$data["s_id"])->find();
		if(empty($subscribe)){
			return toError(0,'s_id操作错误');
		}
		//上门时间
		$visit_time = Db::name("process")->where("t_id",5)->where('status',2)->where("s_id",$subscribe['id'])->value('visit_time');
		
		//装修面积
		$zx_data = Db::name("offer")->where("s_id",$subscribe['id'])->where('status',2)->find();
		
		//项目列表
		$xm_data = Db::name('staff_type')->where('id','in',$subscribe['item_ids'])->column('name');
		
		//项目进度
		$info=[
			'name'=>$subscribe['name'],
			'address'=>$subscribe['address'],
			'order'=>$subscribe['order'],
			'visit_time'=>$visit_time,
			'remark'=>$subscribe['remark'],
			'mianji'=>$zx_data['mianji']?$zx_data['mianji']:0,
			'money'=>$zx_data['money']?$zx_data['money']:0,
			'xiangmu'=>implode(',',$xm_data),
			'jindu'=>$subscribe['x_status']
		];
		return toSuccess('获取成功',$info);
	}
	
	
	
	public function k_user_chat_list(){
		$data=input('post.');
		if(empty($data['user_id'])){
			return toError(0,'操作错误');
		}
		$data1=Db::name('process p')->join('subscribe s','p.s_id=s.id','left')->where(["s.user_id"=>$data['user_id']])
		->join('user u','p.user_id=u.id','left')->field('u.id to_id,u.username,u.head_img,p.t_id')->group('u.id')->where('u.id','not in',$data['user_id'])
		->order('p.add_time desc')->select();
		return toSuccess('请求成功',$data1);
	}
	
	
	public function add_evaluate(){
		$data=input('post.');
		if(empty($data['user_id'])){
			return toError(0,'操作错误');
		}
		if(empty($data['s_id'])){
			return toError(0,'s_id操作错误');
		}
		if(!isset($data['z_evaluate'])){
			return toError(0,'z_evaluate操作错误');
		}
		if(!isset($data['sms_evaluate'])){
			return toError(0,'sms_evaluate操作错误');
		}
		if(!isset($data['smt_evaluate'])){
			return toError(0,'smt_evaluate操作错误');
		}
		if(!isset($data['sjs_evaluate'])){
			return toError(0,'sjs_evaluate操作错误');
		}
		if(!isset($data['sjb_evaluate'])){
			return toError(0,'sjb_evaluate操作错误');
		}
		$data["add_time"]=time();
		if(Db::name("evaluate")->insert($data)){
			return toSuccess('操作成功');
		}else{
			return toError(0,'操作错误');
		}
	}
	
	public function add_complaint(){
		$data=input('post.');
		if(empty($data['user_id'])){
			return toError(0,'操作错误');
		}
		if(empty($data['s_id'])){
			return toError(0,'s_id操作错误');
		}
		
		if(empty($data['content'])){
			return toError(0,'内容不能为空');
		}
		
		if(Db::name("subscribe")->where("id",$data['s_id'])->update(["complaint"=>$data['content']])){
			return toSuccess('提交成功');
		}else{
			return toError(0,'操作错误');
		}
	}
	
	
}
?>